<?php
declare(strict_types=1);

namespace App\Wxc\Dto;

use Hyperf\HttpServer\Contract\RequestInterface;
use Psr\SimpleCache\InvalidArgumentException;
use Throwable;
use Xmo\JWTAuth\JWT;

/**
 * 分享员小程序凭证
 */
class WxcSharerMpToken
{
    protected RequestInterface $request;

    protected JWT $jwt;

    private string $scene = 'wxc_sharer_mp';

    public function __construct()
    {
        $this->jwt = container()->get(JWT::class)->setScene($this->scene);
        $this->request = container()->get(RequestInterface::class);
    }

    /**
     * 验证token.
     */
    public function check(?string $token = null): bool
    {
        try {
            if ($this->jwt->checkToken($token, $this->scene, true, true, true)) {
                return true;
            }
        } catch (Throwable $e) {
            return false;
        }

        return false;
    }

    /**
     * 获取JWT对象
     */
    public function getJwt(): JWT
    {
        return $this->jwt;
    }

    /**
     * 获取Token.
     * @throws InvalidArgumentException
     */
    public function getToken(array $payload): string
    {
        return $this->jwt->getToken($payload);
    }

    /**
     * 刷新token.
     */
    public function refresh(): string
    {
        return $this->jwt->refreshToken();
    }

    public function logout(): bool
    {
        return $this->jwt->logout();
    }

    public function getParserData(?string $token = null): array
    {
        return $this->jwt->getParserData($token);
    }
}